"""
数据处理工具模块
"""
import logging
import pandas as pd
import numpy as np
import json
from datetime import datetime

logger = logging.getLogger(__name__)

class DataUtils:
    """数据处理工具类"""
    
    @staticmethod
    def flatten_json(nested_json, parent_key='', sep='.'):
        """
        扁平化嵌套的JSON
        
        Args:
            nested_json: 嵌套的JSON对象
            parent_key: 父键
            sep: 分隔符
        
        Returns:
            dict: 扁平化后的JSON
        """
        items = {}
        for k, v in nested_json.items():
            new_key = f"{parent_key}{sep}{k}" if parent_key else k
            
            if isinstance(v, dict):
                items.update(DataUtils.flatten_json(v, new_key, sep=sep))
            elif isinstance(v, list):
                for i, item in enumerate(v):
                    if isinstance(item, dict):
                        items.update(DataUtils.flatten_json(item, f"{new_key}{sep}{i}", sep=sep))
                    else:
                        items[f"{new_key}{sep}{i}"] = item
            else:
                items[new_key] = v
        
        return items
    
    @staticmethod
    def unflatten_json(flattened_json, sep='.'):
        """
        还原扁平化的JSON
        
        Args:
            flattened_json: 扁平化的JSON对象
            sep: 分隔符
        
        Returns:
            dict: 嵌套的JSON
        """
        result = {}
        
        for key, value in flattened_json.items():
            parts = key.split(sep)
            d = result
            
            for part in parts[:-1]:
                if part.isdigit():
                    part = int(part)
                
                if part not in d:
                    if isinstance(part, int) or parts[parts.index(part) + 1].isdigit():
                        d[part] = []
                    else:
                        d[part] = {}
                
                d = d[part]
            
            last_part = parts[-1]
            if last_part.isdigit():
                last_part = int(last_part)
            
            d[last_part] = value
        
        return result
    
    @staticmethod
    def convert_types(df, type_mapping):
        """
        转换DataFrame的列类型
        
        Args:
            df: DataFrame
            type_mapping: 类型映射 (列名 -> 类型)
        
        Returns:
            pandas.DataFrame: 转换后的DataFrame
        """
        try:
            # 复制DataFrame
            result = df.copy()
            
            # 转换类型
            for column, dtype in type_mapping.items():
                if column in result.columns:
                    if dtype == 'int':
                        result[column] = pd.to_numeric(result[column], errors='coerce').fillna(0).astype(int)
                    elif dtype == 'float':
                        result[column] = pd.to_numeric(result[column], errors='coerce').fillna(0.0).astype(float)
                    elif dtype == 'bool':
                        result[column] = result[column].map({'true': True, 'false': False, '1': True, '0': False, 1: True, 0: False, True: True, False: False})
                    elif dtype == 'date':
                        result[column] = pd.to_datetime(result[column], errors='coerce')
                    elif dtype == 'str':
                        result[column] = result[column].astype(str)
            
            return result
        except Exception as e:
            logger.error(f"转换类型失败: {str(e)}")
            raise